package sm;

import java.util.Hashtable;

public class ServiceManager {
	 private static ServiceManager ref;
	 private static final int eventId = 1000;	
	 private SensingService  eventObjectTest; 
	 private byte optimalNode;
	  
	 


	 private ServiceManager(){
		 ServiceRegistry.getInstance().registerService("1000", eventId);
		 LightService.getInstance();//as components they register themselves
		 TempService.getInstance();
	 }
	 public static synchronized ServiceManager getInstance(){
		 if (ref == null)
			 ref = new ServiceManager();
		 return ref;
	    }
	
	 public synchronized void serviceRequest(long requestId, String eventId, long sampleFreq, 
			 long duration, long timeTx, String eventIdProc, byte targetId, byte requester){
		 if (isTargetHere(targetId)){
			 optimalNode = this.selectOptimalNode(targetId);
			 EventSelector eventSelector = new EventSelector();
			 SensingService eventObject1 = eventSelector.getEventObject(eventId);
			 eventObjectTest = eventObject1;
			 if((checkAvailability(eventId)) && (eventObject1.isValidRequest(requestId, sampleFreq, duration))){
				 eventObject1.processRequestThread(requestId, sampleFreq, duration);	//calls on the multi-thread version
		      }else{
		          System.out.println("sensing event x not available or request not valid");
		      	}
		 }else {
	 		System.out.println("target not a child of this SM");
		 	}		 
	 	}
	 public synchronized double serviceRequest(long requestId, String eventId, byte targetId, byte requester){
		 if ((!isTargetHere(targetId)) || (!checkAvailability(eventId))){
			 System.out.println(" node not a child or event not available");
		 	}
			 optimalNode = this.selectOptimalNode(targetId);
			 EventSelector eventSelector = new EventSelector();
			 SensingService eventObject2 = eventSelector.getEventObject(eventId);
			 return eventObject2.getValue();	 		
	 	}
	 public synchronized boolean checkAvailability(String event){
		 return ServiceRegistry.isServiceRegistered(event);
	    }
	 public synchronized Hashtable getData(long requestId, String eventId, byte requester){
		 EventSelector eventSelector = new EventSelector();
		 SensingService eventObject3 = eventSelector.getEventObject(eventId);
		 Hashtable dataHash = (Hashtable)(eventObject3.getValue(requestId));
		 return dataHash;		 
	 }
	 private synchronized boolean isTargetHere(byte targetId){
		 return true;// here i will check child registry for this SM. hashtable
	 }
	 public synchronized byte selectOptimalNode(byte targetId){
		 return targetId; // this method should select optimal when target is group
	 }	 
	 public synchronized SensingService getEventObject(){
		 return this.eventObjectTest;
	 }
	 public synchronized Hashtable getAllData(String eventId, byte requester){
		 EventSelector eventSelector = new EventSelector();
		 SensingService eventObject1 = eventSelector.getEventObject(eventId);
		 Hashtable dataHash = (Hashtable)(eventObject1.getAllData());		 
		 return dataHash;
	 }
		

}
